import java.util.Scanner;

public class Demo {
	static Scanner sc = new Scanner(System.in);
	
	static void game(int[][] array){ //function for game 
		int k = 100;
		int row= 0; //array.length;
		int col= array[0].length;
		int [][] userArray = new int [row][col];//array with information for the user
		int count=1;
		while(count>0){
			System.out.print("Enter a row:");
			int userRow = sc.nextInt();
			System.out.print("Enter a col:");
			int userCol = sc.nextInt();
			//check if userRow and userCol are valid
			while (!((userRow>=0&&userRow<userArray.length)&&(userCol>=0&&userCol<userArray[0].length))){
				System.out.println("Please, enter for rows number between 0 and "+(userArray.length-1));
				System.out.println("Please, enter for cols number between 0 and "+(userArray[0].length-1));
				System.out.print("Enter rows:");
				row=sc.nextInt(); //array.length
				System.out.print("Enter colums:");
				col=sc.nextInt(); //array[0].length
			}
			//if it's bomb count =0 and while stops, the game ends
			if(array[userRow][userCol]==-1){
				System.out.println("You lose!");
				count=0;
			}
			else{//other way print userArray with guessed info
				if(array[userRow][userCol]==0){
					markArea(array,userRow,userCol);
					userArray[userRow][userCol]=array[userRow][userCol];
				}else{
					userArray[userRow][userCol]=array[userRow][userCol];
				}
				printArray(array,userArray.length, userArray[0].length);
			}
		}
	
	}
	
	 public static void markArea(int[][] array, int i, int j) {
		  if(array[i][j]==0){
			  array[i][j]=66;
		   //left
		   if(j>0)
		    markArea(array, i, j-1);
		   //left && up
		   if(i>0 && j>0)
		    markArea(array, i-1, j-1);
		   //up
		   if(i>0)
		    markArea(array, i-1, j);
		   //right && up
		   if(i<array.length-1 && j>0)
		    markArea(array, i+1, j-1);
		   //right
		   if(j<array[0].length-1)
		    markArea(array, i, j+1);
		   //right && down
		   if(i<array.length-1 && j<array[0].length-1)
		    markArea(array, i+1, j+1);
		   //down
		   if(i<array.length-1)
		    markArea(array, i+1, j);
		   //left && down
		   if(i<array.length-1 && j>0)
		    markArea(array, i+1, j-1);
		  }
		  if(array[i][j]>0 && array[i][j]<9)
			  array[i][j]=66;
		  return;
		 }

	static int search (int [][]array, int row, int col){ //function for searching mines
		int counter = 0;
		
		if((row - 1 >= 0)&& (array[row - 1][col] == -1)){//search top cell
			counter++;
		}
		if((row+1<array.length)&&(array[row+1][col]==-1)){//search bottom cell
			counter++;
		}
		if((col - 1 >= 0)&& (array[row][col-1] == -1)){//search left cell
			counter++;
		}
		if((col + 1 < array[0].length)&& (array[row][col+1] == -1)){//search right cell
			counter++;
		}
		if((row - 1 >= 0)&&(col - 1>= 0)&&(array[row-1][col-1]==-1)){//search diagonal top-left cell 
			counter++;
		}
		if((row + 1 < array.length)&&(col + 1< array[0].length)&&(array[row+1][col+1]==-1)){//search diagonal bottom-right cell
			counter++;
		}
		if((row - 1 >= 0)&&(col + 1< array[0].length)&&(array[row-1][col+1]==-1)){////search diagonal top-right cell
			counter++;
		}
		if((row +1 < array.length)&&(col - 1>= 0)&&(array[row+1][col-1]==-1)){//search diagonal bottom-left cell
			counter++;
		}
		return counter;
	}
	static void printArray(int [][] array,int row, int col){//function to print array in appropriate way
		System.out.print("       ");
		for (int i=0; i<col; i++){
			System.out.printf("  %2d   ",i);
		}
		System.out.println();
		for(int i= 0; i<row; i++){
			for (int j = -1; j<col; j++){
				if(j==-1){
					System.out.printf("  %2d   ",i);
				}else{
					System.out.printf("[ %2d ] ",array[i][j]);
				}
			}
			System.out.println();
			
		}
	}
	
	public static void main(String[] args) {
		//crating the array
		System.out.print("Enter rows:");
		int row=sc.nextInt(); //array.length
		System.out.print("Enter colums:");
		int col=sc.nextInt(); //array[0].length
		int random;
		//check if rows and cols are larger than 0
		while (!((row>0)&&(col>0))){
			System.out.println("Please, enter a number larger than 0!!!");
			System.out.print("Enter rows:");
			row=sc.nextInt(); //array.length
			System.out.print("Enter colums:");
			col=sc.nextInt(); //array[0].length
		}
		int [][] array = new int [row][col];
		//putting mines in array, Mines are 30% randomness
		for(int i=0; i<row; i++){
			for(int j=0; j<col; j++){
				random = (int)(Math.random()*10);
				if(random<=3){
					array[i][j]=-1;//that's mines
				}
				else{
					array[i][j]=0;//that's not mines
				}
			}
		}
		//counting how many mines we have around the cell
		for(int i=0; i<row; i++){
			for(int j=0; j<col; j++){
				if(array[i][j]==0){
					array[i][j]=search(array,i,j);//calling function search
				}
			}
		}
		
		printArray(array, array.length, array[0].length);
		
		game(array);//that's the function for playing till we choose bomb
		
	}

}